package net.uman.framework.modules.security.springsecurity;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.uman.entity.User;
import net.uman.web.Context;

public class InitFilter implements Filter {
	/**
	 * 判断用户输入的验证码是否正确
	 */
	private static final long serialVersionUID = -5838154525730151323L;

	public void init(FilterConfig config) throws ServletException {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		Context ctx = (Context)request.getSession(true).getAttribute("context");
		if (ctx == null) {
			ctx = new Context();
			request.getSession(true).setAttribute("context", ctx);
		}

		User user = SpringSecurityUtils.getCurrentUser();
		if (user != null) {
			if (ctx.getUser() == null) {
				ctx.setUser(user);
//				UserManager manager = ApplicationContextProvider.getBean(UserManager.class);
//				ctx.setOwner(manager.getDepartment(user));
//				ctx.setSuperUser(manager.isSupervisor(user));

				request.getSession(true).setAttribute("user", user);
			}
		}

		filterChain.doFilter(request, response);
	}
	
//	private Menubar createMenubar(User user, HttpServletRequest servletRequest) {
//		Menubar bar = new Menubar();
//		
//		int type = user.getType();
//		List<Menu> menus = null;
//		switch (type) {
//		case User.TYPE_CHAINHQ:
//			menus = getChainHqMenus(servletRequest);
//			break;
//
//		default:
//			break;
//		}
//		bar.setMenus(menus);
//		
//		return bar;
//	}
//
//	private List<Menu> getChainHqMenus(HttpServletRequest servletRequest) {
//		String ctx = servletRequest.getContextPath();
//		
//		List<Menu> menus = new ArrayList<Menu>();
//		
//		Menu menu = new Menu("店面订货");
//		menus.add(menu);
//		
//		Menu submenu = menu.add(new Menu("订货管理"));		
//		submenu.add(new Menu("订货审核", ctx + "/logistics/assignorder.action?orderType=0&tabIndex=1"));
//		submenu.add(new Menu("订单跟踪", ctx + "/logistics/assignorder.action?orderType=0&tabIndex=0"));
//
//		submenu = menu.add(new Menu("退货管理"));
//		submenu.add(new Menu("退货审核", ctx + "/logistics/assignorder.action?orderType=1&tabIndex=1"));
//		submenu.add(new Menu("退货单跟踪", ctx + "/logistics/assignorder.action?orderType=1&tabIndex=0"));
//		
//		submenu = menu.add(new Menu("毁损管理"));
//		submenu.add(new Menu("收货差异", ctx + "/damage/receivedifferent.action?portal=chainhqstore"));
//		submenu.add(new Menu("审核报损", ctx + "/damage/checkdamage.action?portal=chainhqstore"));
//
//		menu = new Menu("连锁订货");
//		menus.add(menu);
//		
//		submenu = menu.add(new Menu("订货管理"));		
//		submenu.add(new Menu("连锁订货", ctx + "/chainhq/chainhqorder.action"));
//		submenu.add(new Menu("订单跟踪", ctx + "/chainhq/chainorderprocessor.action?tabIndex=0&chainOrderType=0"));
//		
//		return menus;
//	}
}